﻿using H2.NET.Common;
using H2.NET.Web.Pages.SMD.Model;
using H2.NET.Web.Pages.SMD.Service;
using H2.NET.Web.Workflow.DwzCode;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace H2.NET.Web.Management.MeiPing
{
    public partial class DepositImport : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var action = AksRequest.GetQueryString("action");
            if (action == "save")
            {
                if (file.PostedFile.ContentLength != 0)
                {
                    string bs = Util.AppSettings("path");
                    string path = bs + @"\" + file.PostedFile.FileName;
                    var stat = true;
                    try
                    {
                        file.PostedFile.SaveAs(path);
                        string ext = Path.GetExtension(path);
                        if (ext == ".xls" || ext == ".xlsx")
                        {
                            var dealerDepositService = new DealerDepositService();
                            
                            IWorkbook hssfworkbook;
                            using (var fl = new FileStream(path, FileMode.Open, FileAccess.Read))
                            {
                                hssfworkbook = WorkbookFactory.Create(fl);
                            }
                            var sheet = hssfworkbook.GetSheetAt(0);
                            dealerDepositService.DeleteAll();
                            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

                            while (rows.MoveNext())
                            {
                                var row = (IRow)rows.Current;
                                if (row.RowNum != 0) { 
                                var dealerDeposit = new t_smd_DealerDeposit();
                                    dealerDeposit.DealerCode = row.GetCell(0).ToString();
                                    dealerDeposit.Name = row.GetCell(1).ToString();
                                    dealerDeposit.Region = row.GetCell(2).ToString();
                                    dealerDeposit.Amount = Utils.StrToFloat(row.GetCell(3).ToString(),0);
                                    dealerDepositService.Add(dealerDeposit);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        stat = false;
                    }
                    finally
                    {
                        File.Delete(bs + file.PostedFile.FileName);
                    }
                    if (stat)
                    {
                        SuccessMsg("MeiPingIndex");
                    }
                    else
                    {
                        ErrorMsg("导入失败，请检查Excel格式为xls！");
                    }
                    
                }
            }
        }
    }
}